package com.qf.leadnewswemedia.controller.v1.feign;

import com.qf.leadnewsfeignapi.wemedia.WeMediaApi;
import com.qf.leadnewsmodel.pojos.wemedia.WmNews;
import com.qf.leadnewswemedia.model.pojos.WmChannel;
import com.qf.leadnewswemedia.model.pojos.WmUser;
import com.qf.leadnewswemedia.service.WmChannelService;
import com.qf.leadnewswemedia.service.WmNewsService;
import com.qf.leadnewswemedia.service.WmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.PrimitiveIterator;

/**
 * 对外提供的远程调用接口
 * 这个接口资源访问，不需要经过网关，直接在服务器间调用
 */
@RestController
@RequestMapping("/api/feign")
public class WeMediaFeignApi implements WeMediaApi {

    @Autowired
    private WmNewsService wmNewsService;

    @Autowired
    private WmChannelService wmChannelService;

    @Autowired
    private WmUserService wmUserService;

    @GetMapping("/news/getOne/{id}")
    public WmNews findNewsById(@PathVariable("id") Integer id) {
        return wmNewsService.getById(id);
    }

    @PostMapping("/news/update")
    public void updateNewsById(@RequestBody WmNews wmNews) {
        wmNewsService.updateById(wmNews);
    }

    @GetMapping("/channel/findName/{id}")
    public String findChannelNameById(@PathVariable("id") Integer id) {
        WmChannel wmChannel = wmChannelService.getById(id);
        return wmChannel == null?"":wmChannel.getName();
    }

    @GetMapping("/user/findName/{id}")
    public String findUserNameById(@PathVariable("id") Integer id) {
        WmUser wmuser = wmUserService.getById(id);
        return wmuser==null?"":wmuser.getName();
    }
}
